﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.DemandPlan
{
    public partial class BidPlanSendDoc : System.Web.UI.Page
    {
        Maticsoft.BLL.workFlow wm = new Maticsoft.BLL.workFlow();
        Maticsoft.BLL.supplier_bidplan sm = new Maticsoft.BLL.supplier_bidplan();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);

                    if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                    {
                        int dpid = (Convert.ToInt32(Request.Params["id"]));

                        Bind(person_id, dpid);
                    }

                }
            }
        }

        //确认
        protected void btn_confirm_Click(object sender, EventArgs e)
        {
            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);

                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    int dpid = (Convert.ToInt32(Request.Params["id"]));

                    ////更改供应商表状态为已发送
                    for (int i = 0; i < this.GridView1.Rows.Count; i++)
                    {
                        //CheckBox cbo = this.GridView1.Rows[i].FindControl("cbo_check") as CheckBox;

                        //if (cbo != null && cbo.Checked)
                        //{
                            Label lbl = this.GridView1.Rows[i].FindControl("lbl_sid") as Label;

                            if (!string.IsNullOrEmpty(lbl.Text))
                            {
                                int sid = int.Parse(lbl.Text);

                                Maticsoft.Model.supplier_bidplan s = sm.GetModel(sid);

                                s.isSendBidPlan = true;

                                //List<int> nextId = lcs.BLL.Tools.GetNextFlowUserId(person_id, dpid);//获取财务确认人

                                //if (nextId.Count == 1)
                                //{
                                //    s.confirmBidDeposit_user_id = nextId[0];
                                  
                                //}

                                sm.Update(s);

                                if (s.confirmBidDeposit_user_id != null)
                                {
                                    //插入下一节点信息
                                    SetNextFlowNode(dpid, s.confirmBidDeposit_user_id.Value);
                                }
                               
                            }
                       // }

                    }

                    
                    Bind(person_id, dpid);
                }

            }


        }


        //加入下一节点信息
        private void SetNextFlowNode(int dpid, int submitId)
        {
            List<Maticsoft.Model.documentFlowInfo> li = new Maticsoft.BLL.documentFlowInfo().GetModelList(" document_id = " + dpid);

            if (li.Count == 1)
            {
                Maticsoft.Model.documentFlowInfo d = li[0];

                string reason = "同意";
                int person_id = 0;
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);
                }

                //获取当前审批人审批节点号，transfer的undefine4记录的是该由哪个审批人审批的节点号，如果要修改自身的workFlow，则节点号就是undefine4
                int flow_node_num_now = Convert.ToInt32(d.flow_node_num_now);
                int flow_node_id_now = 0;

                flow_node_id_now = wm.GetFlow_node_id_now(person_id, d.document_id.Value, flow_node_num_now);

                if (flow_node_id_now != -1)
                {
                    if (wm.UpdateW(person_id, d.document_id.Value, 1, reason, DateTime.Now, flow_node_id_now, flow_node_num_now))//修改本身意见为同意,添加处理意见、处理时间
                    {
                        int flow_node_id = wm.GetFlow_node_id(person_id, d.document_id.Value, Convert.ToInt32(d.flow_node_num_now),d.flow_id.Value);

                        if (flow_node_id != -1)
                        {
                            DataSet ds = wm.GetList(" flow_node_id = " + flow_node_id);

                            lcs.BLL.Tools.DealWorkFlowSend(d, flow_node_id, submitId, person_id);//发给财务

                        }

                       
                    }

                }

            }
        }

        private void Bind(int uid, int dpid)
        {
            DataSet ds = new Maticsoft.BLL.supplier_bidplan().GetListByView(" submit_user_id =  " + uid + "  and dpid=" + dpid
                + " and isSendBidPlan = 0");

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();

            if (ds.Tables[0].Rows.Count == 0)
            {
                this.btn_confirm.Visible = false;
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType ==  DataControlRowType.DataRow)
            {
                Label lbl_isBidFee = e.Row.FindControl("lbl_isBidFee") as Label;
                CheckBox cbo = e.Row.FindControl("cbo_check") as CheckBox;

                if (lbl_isBidFee.Text == "否")
                {
                    cbo.Checked = false;
                    cbo.Enabled = false;
                    
                }
                else
                {
                    cbo.Enabled = true;
                }
            }
        }
    }
}